home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 320 / compsrc2 / fold-con.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  42.5 KB  |  1,558 lines

  1. /*@@ Fix lossage on folding division of big integers.  */
  2.  
  3. /*@@ This file should be rewritten to use an arbitary precision
  4.   @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
  5.   @@ Perhaps the routines could also be used for bc/dc, and made a lib.
  6.   @@ The routines that translate from the ap rep should
  7.   @@ warn if precision et. al. is lost.
  8.   @@ This would also make life easier when this technology is used
  9.   @@ for cross-compilers.  */
  10.  
  11. /* Fold a constant sub-tree into a single node for C-compiler
  12.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  13.  
  14. This file is part of GNU CC.
  15.  
  16. GNU CC is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY.  No author or distributor
  18. accepts responsibility to anyone for the consequences of using it
  19. or for whether it serves any particular purpose or works at all,
  20. unless he says so in writing.  Refer to the GNU CC General Public
  21. License for full details.
  22.  
  23. Everyone is granted permission to copy, modify and redistribute
  24. GNU CC, but only under the conditions described in the
  25. GNU CC General Public License.   A copy of this license is
  26. supposed to have been given to you along with GNU CC so you
  27. can know your rights and responsibilities.  It should be in a
  28. file named COPYING.  Among other things, the copyright notice
  29. and this notice must be preserved on all copies.  */
  30.  
  31.  
  32. /* There are only two entry points in this file:
  33.    fold and combine.
  34.  
  35.    fold takes a tree as argument and returns a simplified tree.
  36.  
  37.    combine takes a tree code for an arithmetic operation
  38.    and two operands that are trees for constant values
  39.    and returns the result of the specified operation on those values,
  40.    also as a tree.  */
  41.    
  42. #include <stdio.h>
  43. #include "config.h"
  44. #include "tree.h"
  45.  
  46. static void lshift_double ();
  47. static void rshift_double ();
  48. static void lrotate_double ();
  49. static void rrotate_double ();
  50.  
  51. /* To do constant folding on INTEGER_CST nodes requires 64-bit arithmetic.
  52.    We do that by representing the 64-bit integer as 8 shorts,
  53.    with only 8 bits stored in each short, as a positive number.  */
  54.  
  55. /* Unpack a 64-bit integer into 8 shorts.
  56.    LOW and HI are the integer, as two `int' pieces.
  57.    SHORTS points to the array of shorts.  */
  58.  
  59. static void
  60. encode (shorts, low, hi)
  61.      short *shorts;
  62.      int low, hi;
  63. {
  64.   shorts[0] = low & 0xff;
  65.   shorts[1] = (low >> 8) & 0xff;
  66.   shorts[2] = (low >> 16) & 0xff;
  67.   shorts[3] = (low >> 24) & 0xff;
  68.   shorts[4] = hi & 0xff;
  69.   shorts[5] = (hi >> 8) & 0xff;
  70.   shorts[6] = (hi >> 16) & 0xff;
  71.   shorts[7] = (hi >> 24) & 0xff;
  72. }
  73.  
  74. /* Pack an array of 8 shorts into a 64-bit integer.
  75.    SHORTS points to the array of shorts.
  76.    The integer is stored into *LOW and *HI as two `int' pieces.  */
  77.  
  78. static void
  79. decode (shorts, low, hi)
  80.      short *shorts;
  81.      int *low, *hi;
  82. {
  83.   *low = (shorts[3] << 24) | (shorts[2] << 16) | (shorts[1] << 8) | shorts[0];
  84.   *hi = (shorts[7] << 24) | (shorts[6] << 16) | (shorts[5] << 8) | shorts[4];
  85. }
  86.  
  87. /* Make the integer constant T valid for its type
  88.    by setting to 0 or 1 all the bits in the constant
  89.    that don't belong in the type.  */
  90.  
  91. static void
  92. force_fit_type (t)
  93.      tree t;
  94. {
  95.   register int prec = TYPE_PRECISION (TREE_TYPE (t));
  96.  
  97.   if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
  98.     prec = BITS_PER_WORD;
  99.  
  100.   /* First clear all bits that are beyond the type's precision.  */
  101.  
  102.   if (prec > HOST_BITS_PER_INT)
  103.     {
  104.       TREE_INT_CST_HIGH (t)
  105.     &= ~((-1) << (prec - HOST_BITS_PER_INT));
  106.     }
  107.   else
  108.     {
  109.       TREE_INT_CST_HIGH (t) = 0;
  110.       if (prec < HOST_BITS_PER_INT)
  111.     TREE_INT_CST_LOW (t)
  112.       &= ~((-1) << prec);
  113.     }
  114.  
  115.   /* If it's a signed type and value's sign bit is set, extend the sign.  */
  116.  
  117.   if (! TREE_UNSIGNED (TREE_TYPE (t))
  118.       && (prec > HOST_BITS_PER_INT
  119.       ? TREE_INT_CST_HIGH (t) & (1 << (prec - HOST_BITS_PER_INT - 1))
  120.       : TREE_INT_CST_LOW (t) & (1 << (prec - 1))))
  121.     {
  122.       /* Value is negative:
  123.      set to 1 all the bits that are outside this type's precision.  */
  124.       if (prec > HOST_BITS_PER_INT)
  125.     {
  126.       TREE_INT_CST_HIGH (t)
  127.         |= ((-1) << (prec - HOST_BITS_PER_INT));
  128.     }
  129.       else
  130.     {
  131.       TREE_INT_CST_HIGH (t) = -1;
  132.       if (prec < HOST_BITS_PER_INT)
  133.         TREE_INT_CST_LOW (t)
  134.           |= ((-1) << prec);
  135.     }
  136.     }
  137. }
  138.  
  139. /* Add two 64-bit integers with 64-bit result.
  140.    Each argument is given as two `int' pieces.
  141.    One argument is L1 and H1; the other, L2 and H2.
  142.    The value is stored as two `int' pieces in *LV and *HV.
  143.    We use the 8-shorts representation internally.  */
  144.  
  145. static void
  146. add_double (l1, h1, l2, h2, lv, hv)
  147.      int l1, h1, l2, h2;
  148.      int *lv, *hv;
  149. {
  150.   short arg1[8];
  151.   short arg2[8];
  152.   register int carry = 0;
  153.   register int i;
  154.  
  155.   encode (arg1, l1, h1);
  156.   encode (arg2, l2, h2);
  157.  
  158.   for (i = 0; i < 8; i++)
  159.     {
  160.       carry += arg1[i] + arg2[i];
  161.       arg1[i] = carry & 0xff;
  162.       carry >>= 8;
  163.     }
  164.  
  165.   decode (arg1, lv, hv);
  166. }
  167.  
  168. /* Negate a 64-bit integers with 64-bit result.
  169.    The argument is given as two `int' pieces in L1 and H1.
  170.    The value is stored as two `int' pieces in *LV and *HV.
  171.    We use the 8-shorts representation internally.  */
  172.  
  173. static void
  174. neg_double (l1, h1, lv, hv)
  175.      int l1, h1;
  176.      int *lv, *hv;
  177. {
  178.   if (l1 == 0)
  179.     {
  180.       *lv = 0;
  181.       *hv = - h1;
  182.     }
  183.   else
  184.     {
  185.       *lv = - l1;
  186.       *hv = ~ h1;
  187.     }
  188. }
  189.  
  190. /* Multiply two 64-bit integers with 64-bit result.
  191.    Each argument is given as two `int' pieces.
  192.    One argument is L1 and H1; the other, L2 and H2.
  193.    The value is stored as two `int' pieces in *LV and *HV.
  194.    We use the 8-shorts representation internally.  */
  195.  
  196. static void
  197. mul_double (l1, h1, l2, h2, lv, hv)
  198.      int l1, h1, l2, h2;
  199.      int *lv, *hv;
  200. {
  201.   short arg1[8];
  202.   short arg2[8];
  203.   short prod[16];
  204.   register int carry = 0;
  205.   register int i, j, k;
  206.  
  207.   encode (arg1, l1, h1);
  208.   encode (arg2, l2, h2);
  209.  
  210.   bzero (prod, sizeof prod);
  211.  
  212.   for (i = 0; i < 8; i++)
  213.     for (j = 0; j < 8; j++)
  214.       {
  215.     k = i + j;
  216.     carry = arg1[i] * arg2[j];
  217.     while (carry)
  218.       {
  219.         carry += prod[k];
  220.         prod[k] = carry & 0xff;
  221.         carry >>= 8;
  222.         k++;
  223.       }
  224.       }
  225.  
  226.   decode (prod, lv, hv);    /* @@decode ignores prod[8] -> prod[15] */
  227. }
  228.  
  229. /* Shift the 64-bit integer in L1, H1 left by COUNT places
  230.    keeping only PREC bits of result.
  231.    Shift right if COUNT is negative.
  232.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  233.    Store the value as two `int' pieces in *LV and *HV.  */
  234.  
  235. static void
  236. lshift_double (l1, h1, count, prec, lv, hv, arith)
  237.      int l1, h1, count, prec;
  238.      int *lv, *hv;
  239.      int arith;
  240. {
  241.   short arg1[8];
  242.   register int i;
  243.   register int carry;
  244.  
  245.   if (count < 0)
  246.     {
  247.       rshift_double (l1, h1, - count, prec, lv, hv, arith);
  248.       return;
  249.     }
  250.  
  251.   encode (arg1, l1, h1);
  252.   if (prec < HOST_BITS_PER_INT)
  253.     count &= (1 << prec) - 1;
  254.  
  255.   while (count > 0)
  256.     {
  257.       carry = 0;
  258.       for (i = 0; i < 8; i++)
  259.     {
  260.       carry += arg1[i] << 1;
  261.       arg1[i] = carry & 0xff;
  262.       carry >>= 8;
  263.     }
  264.       count--;
  265.     }
  266.  
  267.   decode (arg1, lv, hv);
  268. }
  269.  
  270. /* Shift the 64-bit integer in L1, H1 right by COUNT places
  271.    keeping only PREC bits of result.  COUNT must be positive.
  272.    ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
  273.    Store the value as two `int' pieces in *LV and *HV.  */
  274.  
  275. static void
  276. rshift_double (l1, h1, count, prec, lv, hv, arith)
  277.      int l1, h1, count, prec;
  278.      int *lv, *hv;
  279.      int arith;
  280. {
  281.   short arg1[8];
  282.   register int i;
  283.   register int carry;
  284.  
  285.   encode (arg1, l1, h1);
  286.   if (prec < HOST_BITS_PER_INT)
  287.     count &= (1 << prec) - 1;
  288.  
  289.   carry = arith && arg1[7] >> 7;
  290.   while (count > 0)
  291.     {
  292.       for (i = 7; i >= 0; i--)
  293.     {
  294.       carry <<= 8;
  295.       carry += arg1[i];
  296.       arg1[i] = (carry >> 1) & 0xff;
  297.     }
  298.       count--;
  299.     }
  300.  
  301.   decode (arg1, lv, hv);
  302. }
  303.  
  304. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  305.    keeping only PREC bits of result.
  306.    Rotate right if COUNT is negative.
  307.    Store the value as two `int' pieces in *LV and *HV.  */
  308.  
  309. static void
  310. lrotate_double (l1, h1, count, prec, lv, hv)
  311.      int l1, h1, count, prec;
  312.      int *lv, *hv;
  313. {
  314.   short arg1[8];
  315.   register int i;
  316.   register int carry;
  317.  
  318.   if (count < 0)
  319.     {
  320.       rrotate_double (l1, h1, - count, prec, lv, hv);
  321.       return;
  322.     }
  323.  
  324.   encode (arg1, l1, h1);
  325.   if (prec < HOST_BITS_PER_INT)
  326.     count &= (1 << prec) - 1;
  327.  
  328.   carry = arg1[7] >> 7;
  329.   while (count > 0)
  330.     {
  331.       for (i = 0; i < 8; i++)
  332.     {
  333.       carry += arg1[i] << 1;
  334.       arg1[i] = carry & 0xff;
  335.       carry >>= 8;
  336.     }
  337.       count--;
  338.     }
  339.  
  340.   decode (arg1, lv, hv);
  341. }
  342.  
  343. /* Rotate the 64-bit integer in L1, H1 left by COUNT places
  344.    keeping only PREC bits of result.  COUNT must be positive.
  345.    Store the value as two `int' pieces in *LV and *HV.  */
  346.  
  347. static void
  348. rrotate_double (l1, h1, count, prec, lv, hv)
  349.      int l1, h1, count, prec;
  350.      int *lv, *hv;
  351. {
  352.   short arg1[8];
  353.   register int i;
  354.   register int carry;
  355.  
  356.   encode (arg1, l1, h1);
  357.   if (prec < HOST_BITS_PER_INT)
  358.     count &= (1 << prec) - 1;
  359.  
  360.   carry = arg1[0] & 1;
  361.   while (count > 0)
  362.     {
  363.       for (i = 7; i >= 0; i--)
  364.     {
  365.       carry <<= 8;
  366.       carry += arg1[i];
  367.       arg1[i] = (carry >> 1) & 0xff;
  368.     }
  369.       count--;
  370.     }
  371.  
  372.   decode (arg1, lv, hv);
  373. }
  374.  
  375. /* Divide 64 bit integer LNUM, HNUM by 64 bit integer LDEN, HDEN
  376.    for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
  377.    CODE is a tree code for a kind of division, one of
  378.    TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR and ROUND_DIV_EXPR.
  379.    It controls how the quotient is rounded to a integer.
  380.    UNS nonzero says do unsigned division.  */
  381.  
  382. static void
  383. div_and_round_double (code, uns,
  384.               lnum_orig, hnum_orig, lden_orig, hden_orig,
  385.               lquo, hquo, lrem, hrem)
  386.      enum tree_code code;
  387.      int uns;
  388.      int lnum_orig, hnum_orig;        /* num == numerator == dividend */
  389.      int lden_orig, hden_orig;        /* den == denominator == divisor */
  390.      int *lquo, *hquo, *lrem, *hrem;
  391. {
  392.   int quo_neg = 0;
  393.   short num[9], den[8], quo[8];    /* extra element for scaling.  */
  394.   register int i, j, work;
  395.   register int carry = 0;
  396.   int lnum = lnum_orig, hnum = hnum_orig;
  397.   int lden = lden_orig, hden = hden_orig;
  398.  
  399.   if ((hden == 0) && (lden == 0)) {
  400.     *hquo = *lquo = *hrem = *lrem = 0;
  401.     error
  402.       ("divide by 0 in constant folding - quotient and remainder set to 0.");
  403.     return;
  404.   }
  405.  
  406.   /* calculate quotient sign and convert operands to unsigned.  */
  407.   if (!uns) 
  408.     {
  409.       if (hden < 0) 
  410.     {
  411.       quo_neg = ~ quo_neg;
  412.       neg_double (lden, hden, &lden, &hden);
  413.     }
  414.       if (hnum < 0)
  415.     {
  416.       quo_neg = ~ quo_neg;
  417.       neg_double (lnum, hnum, &lnum, &hnum);
  418.     }
  419.     }
  420.  
  421.   if (hnum == 0 && hden == 0)
  422.     {                /* single precision */
  423.       *hquo = *hrem = 0;
  424.       *lquo = (unsigned) lnum / lden;    /* rounds toward zero since positive args */
  425.       goto finish_up;
  426.     }
  427.  
  428.   if (hnum == 0)
  429.     {                /* trivial case: dividend < divisor */
  430.       /* hden != 0 already checked.  */
  431.       *hquo = *lquo = 0;
  432.       *hrem = hnum;
  433.       *lrem = lnum;
  434.       goto finish_up;
  435.     }
  436.  
  437.   bzero (quo, sizeof quo);
  438.  
  439.   bzero (num, sizeof num);    /* to zero 9th element */
  440.   bzero (den, sizeof den);
  441.  
  442.   encode (num, lnum, hnum); 
  443.   encode (den, lden, hden);
  444.  
  445.   if (hden == 0)
  446.     {                /* simpler algorithm */
  447.       /* hnum != 0 already checked.  */
  448.       for (i = 7; i >= 0; i--)
  449.     {
  450.       work = num[i] + (carry << 8);
  451.       quo[i] = work / lden;
  452.       carry = work % lden;
  453.     }
  454.     }
  455.   else {            /* full double precision,
  456.                    with thanks to Don Knuth's
  457.                    "Semi-Numericial Algorithms".  */
  458. #define BASE 256
  459.     int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig;
  460.  
  461.     /* Find the highest non-zero divisor digit.  */
  462.     for (i = 7; ; i--)
  463.       if (den[i] != 0) {
  464.     den_hi_sig = i;
  465.     break;
  466.       }
  467.     for (i = 7; ; i--)
  468.       if (num[i] != 0) {
  469.     num_hi_sig = i;
  470.     break;
  471.       }
  472.     quo_hi_sig = num_hi_sig - den_hi_sig + 1;
  473.  
  474.     /* Insure that the first digit of the divisor is at least BASE/2.
  475.        This is required by the quotient digit estimation algorithm.  */
  476.  
  477.     scale = BASE / (den[den_hi_sig] + 1);
  478.     if (scale > 1) {        /* scale divisor and dividend */
  479.       carry = 0;
  480.       for (i = 0; i <= 8; i++) {
  481.     work = (num[i] * scale) + carry;
  482.     num[i] = work & 0xff;
  483.     carry = work >> 8;
  484.     if (num[i] != 0) num_hi_sig = i;
  485.       }
  486.       carry = 0;
  487.       for (i = 0; i <= 7; i++) {
  488.     work = (den[i] * scale) + carry;
  489.     den[i] = work & 0xff;
  490.     carry = work >> 8;
  491.     if (den[i] != 0) den_hi_sig = i;
  492.       }
  493.     }
  494.  
  495.     /* Main loop */
  496.     for (i = quo_hi_sig; i > 0; i--) {
  497.       /* quess the next quotient digit, quo_est, by dividing the first
  498.      two remaining dividend digits by the high order quotient digit.
  499.      quo_est is never low and is at most 2 high.  */
  500.  
  501.       int num_hi;        /* index of highest remaining dividend digit */
  502.  
  503.       num_hi = i + den_hi_sig;
  504.  
  505.       work = (num[num_hi] * BASE) + (num_hi ? 0 : num[num_hi - 1]);
  506.       if (num[num_hi] != den[den_hi_sig]) {
  507.     quo_est = work / den[den_hi_sig];
  508.       }
  509.       else {
  510.     quo_est = BASE - 1;
  511.       }
  512.  
  513.       /* refine quo_est so it's usually correct, and at most one high.   */
  514.       while ((den[den_hi_sig - 1] * quo_est)
  515.          > (((work - (quo_est * den[den_hi_sig])) * BASE)
  516.          + ((num_hi - 1) ? 0 : num[num_hi - 2]))) {
  517.     quo_est--;
  518.       }
  519.  
  520.       /* try quo_est as the quotient digit, by multiplying the
  521.          divisor by quo_est and subtracting from the remaining dividend.  */
  522.  
  523.       carry = 0;
  524.  
  525.       for (j = 0; j <= den_hi_sig; j++) {
  526.     int digit;
  527.  
  528.     work = num[i + j] - (quo_est * den[j]) + carry;
  529.     digit = work & 0xff;
  530.     carry = work >> 8;
  531.     if (digit < 0) {
  532.       digit += BASE;
  533.       carry--;
  534.     }
  535.     num[i + j] = digit;
  536.       }
  537.  
  538.       /* if quo_est was high by one, then num[i] went negative and
  539.      we need to correct things.  */
  540.  
  541.       if (num[num_hi] < 0) {
  542.     quo_est--;
  543.     carry = 0;        /* add divisor back in */
  544.     for (j = 0; j <= den_hi_sig; j++) {
  545.       work = num[i + j] + den[j] + carry;
  546.       if (work > BASE) {
  547.         work -= BASE;
  548.         carry = 1;
  549.       }
  550.       else {
  551.         carry = 0;
  552.       }
  553.       num[i + j] = work;
  554.     }
  555.     num [num_hi] += carry;
  556.       }
  557.  
  558.       /* store the quotient digit.  */
  559.       quo[i - 1] = quo_est;
  560.     }
  561.   }
  562.  
  563.   decode (quo, lquo, hquo);
  564.  
  565.  finish_up:
  566.   /* if result is negative, make it so.  */
  567.   if (quo_neg)
  568.     neg_double (*lquo, *hquo, lquo, hquo);
  569.  
  570.   /* compute trial remainder:  rem = num - (quo * den)  */
  571.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  572.   neg_double (*lrem, *hrem, lrem, hrem);
  573.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  574.  
  575.   switch (code)
  576.     {
  577.     case TRUNC_DIV_EXPR:
  578.     case TRUNC_MOD_EXPR:    /* round toward zero */
  579.       return;
  580.  
  581.     case FLOOR_DIV_EXPR:
  582.     case FLOOR_MOD_EXPR:    /* round toward negative infinity */
  583.       if (quo_neg && (*lrem != 0 || *hrem != 0))   /* ratio < 0 && rem != 0 */
  584.     {
  585.       /* quo = quo - 1;  */
  586.       add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  587.     }
  588.       else return;
  589.       break;
  590.  
  591.     case CEIL_DIV_EXPR:
  592.     case CEIL_MOD_EXPR:        /* round toward positive infinity */
  593.       if (!quo_neg && (*lrem != 0 || *hrem != 0))  /* ratio > 0 && rem != 0 */
  594.     {
  595.       add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  596.     }
  597.       else return;
  598.       break;
  599.     
  600.     case ROUND_DIV_EXPR:
  601.     case ROUND_MOD_EXPR:    /* round to closest integer */
  602.       {
  603.     int labs_rem = *lrem, habs_rem = *hrem;
  604.     int labs_den = lden, habs_den = hden, ltwice, htwice;
  605.  
  606.     /* get absolute values */
  607.     if (*hrem < 0) neg_double(*lrem, *hrem, &labs_rem, &habs_rem);
  608.     if (hden < 0) neg_double(lden, hden, &labs_den, &habs_den);
  609.  
  610.     /* if (2 * abs (lrem) >= abs (lden)) */
  611.     mul_double(2, 0, labs_rem, habs_rem, <wice, &htwice);
  612.     if (((unsigned) habs_den < (unsigned) htwice)
  613.         || (((unsigned) habs_den == (unsigned) htwice)
  614.         && ((unsigned) labs_den < (unsigned) ltwice)))
  615.       {
  616.         if (*hquo < 0)
  617.           /* quo = quo - 1;  */
  618.           add_double (*lquo, *hquo, -1, -1, lquo, hquo);
  619.         else
  620.           /* quo = quo + 1; */
  621.           add_double (*lquo, *hquo, 1, 0, lquo, hquo);
  622.       }
  623.     else return;
  624.       }
  625.       break;
  626.  
  627.     default:
  628.       abort ();
  629.     }
  630.  
  631.   /* compute true remainder:  rem = num - (quo * den)  */
  632.   mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
  633.   neg_double (*lrem, *hrem, lrem, hrem);
  634.   add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
  635. }
  636.  
  637. /* Split a tree IN into a constant and a variable part
  638.    that could be combined with CODE to make IN.
  639.    CODE must be a commutative arithmetic operation.
  640.    Store the constant part into *CONP and the variable in &VARP.
  641.    Return 1 if this was done; zero means the tree IN did not decompose
  642.    this way.
  643.  
  644.    If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR.
  645.    Therefore, we must tell the caller whether the variable part
  646.    was subtracted.  We do this by storing 1 or -1 into *VARSIGNP.
  647.    The value stored is the coefficient for the variable term.
  648.    The constant term we return should always be added;
  649.    we negate it if necessary.  */
  650.  
  651. static int
  652. split_tree (in, code, varp, conp, varsignp)
  653.      tree in;
  654.      enum tree_code code;
  655.      tree *varp, *conp;
  656.      int *varsignp;
  657. {
  658.   register tree outtype = TREE_TYPE (in);
  659.   *varp = 0;
  660.   *conp = 0;
  661.  
  662.   /* Strip any conversions that don't change the machine mode.  */
  663.   while ((TREE_CODE (in) == NOP_EXPR
  664.       || TREE_CODE (in) == CONVERT_EXPR)
  665.      && (TYPE_MODE (TREE_TYPE (in))
  666.          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0)))))
  667.     in = TREE_OPERAND (in, 0);
  668.  
  669.   if (TREE_CODE (in) == code
  670.       || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE
  671.       /* We can associate addition and subtraction together
  672.          (even though the C standard doesn't say so)
  673.          for integers because the value is not affected.
  674.          For reals, the value might be affected, so we can't.  */
  675.       &&
  676.       ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
  677.        || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
  678.     {
  679.       enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0));
  680.       if (code == INTEGER_CST || code == REAL_CST)
  681.     {
  682.       *conp = TREE_OPERAND (in, 0);
  683.       *varp = TREE_OPERAND (in, 1);
  684.       if (TREE_TYPE (*varp) != outtype)
  685.         *varp = convert (outtype, *varp);
  686.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  687.       return 1;
  688.     }
  689.       if (TREE_LITERAL (TREE_OPERAND (in, 1)))
  690.     {
  691.       *conp = TREE_OPERAND (in, 1);
  692.       *varp = TREE_OPERAND (in, 0);
  693.       *varsignp = 1;
  694.       if (TREE_TYPE (*varp) != outtype)
  695.         *varp = convert (outtype, *varp);
  696.       if (TREE_CODE (in) == MINUS_EXPR)
  697.         {
  698.           /* If operation is subtraction and constant is second,
  699.          must negate it to get an additive constant.
  700.          And this cannot be done unless it is a manifest constant.
  701.          It could also be the address of a static variable.
  702.          We cannot negate that, so give up.  */
  703.           if (TREE_CODE (*conp) == INTEGER_CST
  704.           || TREE_CODE (*conp) == REAL_CST)
  705.         *conp = combine (MINUS_EXPR, integer_zero_node, *conp);
  706.           else
  707.         return 0;
  708.         }
  709.       return 1;
  710.     }
  711.       if (TREE_LITERAL (TREE_OPERAND (in, 0)))
  712.     {
  713.       *conp = TREE_OPERAND (in, 0);
  714.       *varp = TREE_OPERAND (in, 1);
  715.       if (TREE_TYPE (*varp) != outtype)
  716.         *varp = convert (outtype, *varp);
  717.       *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1;
  718.       return 1;
  719.     }
  720.     }
  721.   return 0;
  722. }
  723.  
  724. /* Combine two constants NUM and ARG2 under operation CODE
  725.    to produce a new constant.
  726.    We assume ARG1 and ARG2 have the same data type,
  727.    or at least are the same kind of constant and the same machine mode.  */
  728.  
  729. tree
  730. combine (code, arg1, arg2)
  731.      enum tree_code code;
  732.      register tree arg1, arg2;
  733. {
  734.   if (TREE_CODE (arg1) == INTEGER_CST)
  735.     {
  736.       register int int1l = TREE_INT_CST_LOW (arg1);
  737.       register int int1h = TREE_INT_CST_HIGH (arg1);
  738.       int int2l = TREE_INT_CST_LOW (arg2);
  739.       int int2h = TREE_INT_CST_HIGH (arg2);
  740.       int low, hi;
  741.       int garbagel, garbageh;
  742.       register tree t;
  743.       int uns = TREE_UNSIGNED (TREE_TYPE (arg1));
  744.  
  745.       switch (code)
  746.     {
  747.     case BIT_IOR_EXPR:
  748.       t = build_int_2 (int1l | int2l, int1h | int2h);
  749.       break;
  750.  
  751.     case BIT_XOR_EXPR:
  752.       t = build_int_2 (int1l ^ int2l, int1h ^ int2h);
  753.       break;
  754.  
  755.     case BIT_AND_EXPR:
  756.       t = build_int_2 (int1l & int2l, int1h & int2h);
  757.       break;
  758.  
  759.     case BIT_ANDTC_EXPR:
  760.       t = build_int_2 (int1l & ~int2l, int1h & ~int2h);
  761.       break;
  762.  
  763.     case RSHIFT_EXPR:
  764.       int2l = - int2l;
  765.     case LSHIFT_EXPR:
  766.       lshift_double (int1l, int1h, int2l,
  767.              TYPE_PRECISION (TREE_TYPE (arg1)),
  768.              &low, &hi,
  769.              !uns);
  770.       t = build_int_2 (low, hi);
  771.       break;
  772.  
  773.     case RROTATE_EXPR:
  774.       int2l = - int2l;
  775.     case LROTATE_EXPR:
  776.       lrotate_double (int1l, int1h, int2l,
  777.               TYPE_PRECISION (TREE_TYPE (arg1)),
  778.               &low, &hi);
  779.       t = build_int_2 (low, hi);
  780.       break;
  781.  
  782.     case PLUS_EXPR:
  783.       add_double (int1l, int1h, int2l, int2h, &low, &hi);
  784.       t = build_int_2 (low, hi);
  785.       break;
  786.  
  787.     case MINUS_EXPR:
  788.       neg_double (int2l, int2h, &int2l, &int2h);
  789.       add_double (int1l, int1h, int2l, int2h, &low, &hi);
  790.       t = build_int_2 (low, hi);
  791.       break;
  792.  
  793.     case MULT_EXPR:
  794.       mul_double (int1l, int1h, int2l, int2h, &low, &hi);
  795.       t = build_int_2 (low, hi);
  796.       break;
  797.  
  798.     case TRUNC_DIV_EXPR: case ROUND_DIV_EXPR: 
  799.     case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
  800.       div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  801.                 &low, &hi, &garbagel, &garbageh);
  802.       t = build_int_2 (low, hi);
  803.       break;
  804.  
  805.     case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: 
  806.     case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
  807.       div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
  808.                 &garbagel, &garbageh, &low, &hi);
  809.       t = build_int_2 (low, hi);
  810.       break;
  811.  
  812.     case MIN_EXPR:
  813.     case MAX_EXPR:
  814.       if (uns)
  815.         {
  816.           low = (((unsigned) int1h < (unsigned) int2h)
  817.              || (((unsigned) int1h == (unsigned) int2h)
  818.              && ((unsigned) int1l < (unsigned) int2l)));
  819.         }
  820.       else
  821.         {
  822.           low = ((int1h < int2h)
  823.              || ((int1h == int2h)
  824.              && ((unsigned) int1l < (unsigned) int2l)));
  825.         }
  826.       if (low == (code == MIN_EXPR))
  827.         t = build_int_2 (int1l, int1h);
  828.       else
  829.         t = build_int_2 (int2l, int2h);
  830.       break;
  831.  
  832.     default:
  833.       abort ();
  834.     }
  835.       TREE_TYPE (t) = TREE_TYPE (arg1);
  836.       force_fit_type (t);
  837.       return t;
  838.     }
  839.   if (TREE_CODE (arg1) == REAL_CST)
  840.     {
  841.       register double d1 = TREE_REAL_CST (arg1);
  842.       register double d2 = TREE_REAL_CST (arg2);
  843.       register double value;
  844.  
  845.       switch (code)
  846.     {
  847.     case PLUS_EXPR:
  848.       value = d1 + d2;
  849.       break;
  850.  
  851.     case MINUS_EXPR:
  852.       value = d1 - d2;
  853.       break;
  854.  
  855.     case MULT_EXPR:
  856.       value = d1 * d2;
  857.       break;
  858.  
  859.     case RDIV_EXPR:
  860.       if (d2 == 0)
  861.         return 0;
  862.  
  863.       value = d1 / d2;
  864.       break;
  865.  
  866.     case MIN_EXPR:
  867.       value = d1 < d2 ? d1 : d2;
  868.       break;
  869.  
  870.     case MAX_EXPR:
  871.       value = d1 > d2 ? d1 : d2;
  872.       break;
  873.  
  874.     default:
  875.       abort ();
  876.     }
  877.       return build_real (TREE_TYPE (arg1), value);
  878.     }
  879.   if (TREE_CODE (arg1) == COMPLEX_CST)
  880.     {
  881.       register tree r1 = TREE_REALPART (arg1);
  882.       register tree i1 = TREE_IMAGPART (arg1);
  883.       register tree r2 = TREE_REALPART (arg2);
  884.       register tree i2 = TREE_IMAGPART (arg2);
  885.       register tree t;
  886.  
  887.       switch (code)
  888.     {
  889.     case PLUS_EXPR:
  890.       t = build_complex (combine (PLUS_EXPR, r1, r2),
  891.                  combine (PLUS_EXPR, i1, i2));
  892.       break;
  893.  
  894.     case MINUS_EXPR:
  895.       t = build_complex (combine (MINUS_EXPR, r1, r2),
  896.                  combine (MINUS_EXPR, i1, i2));
  897.       break;
  898.  
  899.     case MULT_EXPR:
  900.       t = build_complex (combine (MINUS_EXPR,
  901.                       combine (MULT_EXPR, r1, r2),
  902.                       combine (MULT_EXPR, i1, i2)),
  903.                  combine (PLUS_EXPR,
  904.                       combine (MULT_EXPR, r1, i2),
  905.                       combine (MULT_EXPR, i1, r2)));
  906.       break;
  907.  
  908.     case RDIV_EXPR:
  909.       {
  910.         register tree magsquared
  911.           = combine (PLUS_EXPR,
  912.              combine (MULT_EXPR, r2, r2),
  913.              combine (MULT_EXPR, i2, i2));
  914.         t = build_complex (combine (RDIV_EXPR,
  915.                     combine (PLUS_EXPR,
  916.                          combine (MULT_EXPR, r1, r2),
  917.                          combine (MULT_EXPR, i1, i2)),
  918.                     magsquared),
  919.                    combine (RDIV_EXPR,
  920.                     combine (MINUS_EXPR,
  921.                          combine (MULT_EXPR, i1, r2),
  922.                          combine (MULT_EXPR, r1, i2)),
  923.                     magsquared));
  924.       }
  925.       break;
  926.  
  927.     default:
  928.       abort ();
  929.     }
  930.       TREE_TYPE (t) = TREE_TYPE (arg1);
  931.       return t;
  932.     }
  933.   return 0;
  934. }
  935.  
  936. /* Given T, a tree representing type conversion of a constant,
  937.    return a constant tree representing the result of conversion.  */
  938.  
  939. static tree
  940. fold_convert (t)
  941.      register tree t;
  942. {
  943.   register tree arg1 = TREE_OPERAND (t, 0);
  944.   register tree type = TREE_TYPE (t);
  945.  
  946.   if (TREE_CODE (type) == POINTER_TYPE
  947.       || TREE_CODE (type) == INTEGER_TYPE
  948.       || TREE_CODE (type) == ENUMERAL_TYPE)
  949.     {
  950.       if (TREE_CODE (arg1) == INTEGER_CST)
  951.     {
  952.       /* Given an integer constant, make new constant with new type,
  953.          appropriately sign-extended or truncated.  */
  954.       t = build_int_2 (TREE_INT_CST_LOW (arg1),
  955.                TREE_INT_CST_HIGH (arg1));
  956.       TREE_TYPE (t) = type;
  957.       force_fit_type (t);
  958.     }
  959.       else if (TREE_CODE (arg1) == REAL_CST)
  960.     t = build_int_2 ((int) TREE_REAL_CST (arg1),
  961.              (int) (TREE_REAL_CST (arg1) / 0x10000 / 0x10000));
  962.       TREE_TYPE (t) = type;
  963.     }
  964.   else if (TREE_CODE (type) == REAL_TYPE)
  965.     {
  966.       if (TREE_CODE (arg1) == INTEGER_CST)
  967.     return build_real_from_int_cst (type, arg1);
  968.       else if (TREE_CODE (arg1) == REAL_CST)
  969.     return build_real (type, TREE_REAL_CST (arg1));
  970.     }
  971.   TREE_LITERAL (t) = 1;
  972.   return t;
  973. }
  974.  
  975. /* Return nonzero if two constants (that are not manifest constants)
  976.    are necessarily equal.  It detects only the easiest, common case of
  977.    equality.  */
  978.  
  979. static int
  980. operand_equal_p (arg0, arg1)
  981.      tree arg0, arg1;
  982. {
  983.   while ((TREE_CODE (arg0) == NOP_EXPR
  984.       || TREE_CODE (arg0) == CONVERT_EXPR)
  985.      && TYPE_MODE (TREE_TYPE (arg0)) == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  986.     arg0 = TREE_OPERAND (arg0, 0);
  987.   while ((TREE_CODE (arg1) == NOP_EXPR
  988.       || TREE_CODE (arg1) == CONVERT_EXPR)
  989.      && TYPE_MODE (TREE_TYPE (arg1)) == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  990.     arg1 = TREE_OPERAND (arg1, 0);
  991.  
  992.   if (TREE_CODE (arg0) == TREE_CODE (arg1)
  993.       && TREE_CODE (arg0) == ADDR_EXPR
  994.       && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0))
  995.     return 1;
  996.   return 0;
  997. }
  998.  
  999. /* Perform constant folding and related simplification of EXPR.
  1000.    The related simplifications include x*1 => x, x*0 => 0, etc.,
  1001.    and application of the associative law.
  1002.    NOP_EXPR conversions may be removed freely (as long as we
  1003.    are careful not to change the C type of the overall expression)
  1004.    We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
  1005.    but we can constant-fold them if they have constant operands.  */
  1006.  
  1007. tree
  1008. fold (expr) 
  1009.      tree expr;
  1010. {
  1011.   register tree t = expr;
  1012.   register tree arg0, arg1;
  1013.   register enum tree_code code = TREE_CODE (t);
  1014.   register int kind;
  1015.  
  1016.   /* WINS will be nonzero when the switch is done
  1017.      if all operands are constant.
  1018.  
  1019.      LOSES will be nonzero when the switch is done
  1020.      if any operand is volatile.
  1021.      This inhibits optimizations such as  (foo () * 0) => 0.
  1022.      But identity-element optimizations such as
  1023.      (foo () * 1) => (foo ()) can be done even if LOSES is set.  */
  1024.  
  1025.   int wins = 1;
  1026.   int loses = 0;
  1027.  
  1028.   /* Return right away if already constant.  */
  1029.   if (TREE_LITERAL (t))
  1030.     {
  1031.       if (code == CONST_DECL)
  1032.     return DECL_INITIAL (t);
  1033.       return t;
  1034.     }
  1035.   
  1036.   kind = *tree_code_type[(int) code];
  1037.   if (kind == 'e' || kind == 'r')
  1038.     {
  1039.       register int len = tree_code_length[(int) code];
  1040.       register int i;
  1041.       for (i = 0; i < len; i++)
  1042.     {
  1043.       if (TREE_OPERAND (t, i) == 0)
  1044.         continue;        /* Valid for CALL_EXPR, at least.  */
  1045.       if (TREE_CODE (TREE_OPERAND (t, i)) != INTEGER_CST
  1046.           && TREE_CODE (TREE_OPERAND (t, i)) != REAL_CST)
  1047.         /* Note that TREE_LITERAL isn't enough:
  1048.            static var addresses are constant but we can't
  1049.            do arithmetic on them.  */
  1050.         wins = 0;
  1051.       if (TREE_VOLATILE (TREE_OPERAND (t, i)))
  1052.         loses = 1;
  1053.     }
  1054.       arg0 = TREE_OPERAND (t, 0);
  1055.       if (len > 1)
  1056.     arg1 = TREE_OPERAND (t, 1);
  1057.     }
  1058.  
  1059.   /* Now WINS and LOSES are set as described above,
  1060.      ARG0 is the first operand of EXPR,
  1061.      and ARG1 is the second operand (if it has more than one operand).  */
  1062.  
  1063.   switch (code)
  1064.     {
  1065.     case INTEGER_CST:
  1066.     case REAL_CST:
  1067.     case STRING_CST:
  1068.     case COMPLEX_CST:
  1069.     case CONSTRUCTOR:
  1070.       return t;
  1071.  
  1072.     case CONST_DECL:
  1073.       return fold (DECL_INITIAL (t));
  1074.  
  1075.     case NOP_EXPR:
  1076.     case FLOAT_EXPR:
  1077.     case CONVERT_EXPR:
  1078.     case FIX_TRUNC_EXPR:
  1079.       /* Other kinds of FIX are not handled properly by fold_convert.  */
  1080.       if (!wins)
  1081.     {
  1082.       TREE_LITERAL (t) = TREE_LITERAL (arg0);
  1083.       return t;
  1084.     }
  1085.       return fold_convert (t);
  1086.  
  1087.     case RANGE_EXPR:
  1088.       TREE_LITERAL (t) = wins;
  1089.       return t;
  1090.  
  1091.     case NEGATE_EXPR:
  1092.       if (wins)
  1093.     {
  1094.       if (TREE_CODE (arg0) == INTEGER_CST)
  1095.         {
  1096.           if (TREE_INT_CST_LOW (arg0) == 0)
  1097.         t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  1098.           else
  1099.         t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  1100.                  ~ TREE_INT_CST_HIGH (arg0));
  1101.           force_fit_type (t);
  1102.         }
  1103.       else if (TREE_CODE (arg0) == REAL_CST)
  1104.         t = build_real (TREE_TYPE (expr), - TREE_REAL_CST (arg0));
  1105.       TREE_TYPE (t) = TREE_TYPE (expr);
  1106.     }
  1107.       return t;
  1108.  
  1109.     case ABS_EXPR:
  1110.       if (wins)
  1111.     {
  1112.       if (TREE_CODE (arg0) == INTEGER_CST)
  1113.         {
  1114.           if (! TREE_UNSIGNED (TREE_TYPE (expr))
  1115.           || TREE_INT_CST_HIGH (arg0) < 0)
  1116.         {
  1117.           if (TREE_INT_CST_LOW (arg0) == 0)
  1118.             t = build_int_2 (0, - TREE_INT_CST_HIGH (arg0));
  1119.           else
  1120.             t = build_int_2 (- TREE_INT_CST_LOW (arg0),
  1121.                      ~ TREE_INT_CST_HIGH (arg0));
  1122.         }
  1123.         }
  1124.       else if (TREE_CODE (arg0) == REAL_CST)
  1125.         {
  1126.           if (TREE_REAL_CST (arg0) < 0)
  1127.         t = build_real (TREE_TYPE (expr), - TREE_REAL_CST (arg0));
  1128.         }
  1129.       TREE_TYPE (t) = TREE_TYPE (expr);
  1130.     }
  1131.       return t;
  1132.  
  1133.     case BIT_NOT_EXPR:
  1134.       if (wins)
  1135.     {
  1136.       if (TREE_CODE (arg0) == INTEGER_CST)
  1137.         t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
  1138.                  ~ TREE_INT_CST_HIGH (arg0));
  1139.       TREE_TYPE (t) = TREE_TYPE (expr);
  1140.       force_fit_type (t);
  1141.     }
  1142.       return t;
  1143.  
  1144.     case PLUS_EXPR:
  1145.       if (integer_zerop (arg0))
  1146.     return convert (TREE_TYPE (expr), arg1);
  1147.       if (integer_zerop (arg1))
  1148.     return convert (TREE_TYPE (expr), arg0);
  1149.     associate:
  1150.       /* In most languages, can't associate operations on floats
  1151.      through parentheses.  Rather than remember where the parentheses
  1152.      were, we don't associate floats at all.  It shouldn't matter much.  */
  1153.       if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
  1154.     goto binary;
  1155.       /* The varsign == -1 cases happen only for addition and subtraction.
  1156.      It says that the arg that was split was really CON minus VAR.
  1157.      The rest of the code applies to all associative operations.  */
  1158.       if (!wins)
  1159.     {
  1160.       tree var, con, tem;
  1161.       int varsign;
  1162.       tree inner_arg;
  1163.  
  1164.       if (split_tree (arg0, code, &var, &con, &varsign))
  1165.         {
  1166.           if (varsign == -1)
  1167.         {
  1168.           /* EXPR is (CON-VAR) +- ARG1.  */
  1169.           /* If it is + and VAR==ARG1, return just CONST.  */
  1170.           if (code == PLUS_EXPR && operand_equal_p (var, arg1))
  1171.             return con;
  1172.             
  1173.           /* Otherwise return (CON +- ARG1) - VAR.  */
  1174.           TREE_SET_CODE (t, MINUS_EXPR);
  1175.           TREE_OPERAND (t, 1) = var;
  1176.           TREE_OPERAND (t, 0)
  1177.             = fold (build (code, TREE_TYPE (t), con, arg1));
  1178.         }
  1179.           else
  1180.         {
  1181.           /* EXPR is (VAR+CON) +- ARG1.  */
  1182.           /* If it is - and VAR==ARG1, return just CONST.  */
  1183.           if (code == MINUS_EXPR && operand_equal_p (var, arg1))
  1184.             return con;
  1185.             
  1186.           /* Otherwise return VAR +- (ARG1 +- CON).  */
  1187.           TREE_OPERAND (t, 1) = tem
  1188.             = fold (build (code, TREE_TYPE (t), arg1, con));
  1189.           TREE_OPERAND (t, 0) = var;
  1190.           if (integer_zerop (tem)
  1191.               && (code == PLUS_EXPR || code == MINUS_EXPR))
  1192.             return var;
  1193.           /* If we have x +/- (c - d) [c an explicit integer]
  1194.              change it to x -/+ (d - c) since if d is relocatable
  1195.              then the latter can be a single immediate insn
  1196.              and the former cannot.  */
  1197.           if (TREE_CODE (tem) == MINUS_EXPR
  1198.               && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST)
  1199.             {
  1200.               tree tem1 = TREE_OPERAND (tem, 1);
  1201.               TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0);
  1202.               TREE_OPERAND (tem, 0) = tem1;
  1203.               TREE_SET_CODE (t,
  1204.                      (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  1205.             }
  1206.         }
  1207.           return t;
  1208.         }
  1209.  
  1210.       if (split_tree (arg1, code, &var, &con, &varsign))
  1211.         {
  1212.           /* EXPR is ARG0 +- (CON +- VAR).  */
  1213.           if (varsign == -1)
  1214.         TREE_SET_CODE (t,
  1215.                    (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR));
  1216.           if (TREE_CODE (t) == MINUS_EXPR && operand_equal_p (var, arg0))
  1217.         return con;
  1218.           TREE_OPERAND (t, 0)
  1219.         = fold (build (code, TREE_TYPE (t), arg0, con));
  1220.           TREE_OPERAND (t, 1) = var;
  1221.           if (integer_zerop (TREE_OPERAND (t, 0))
  1222.           && TREE_CODE (t) == PLUS_EXPR)
  1223.         return var;
  1224.           return t;
  1225.         }
  1226.     }
  1227.     binary:
  1228.       {
  1229.     register tree t1 = NULL_TREE;
  1230.     if (wins)
  1231.       t1 = combine (code, arg0, arg1);
  1232.     if (t1 != NULL_TREE) return t1;
  1233.     return t;
  1234.       }
  1235.  
  1236.     case MINUS_EXPR:
  1237.       if (! wins && integer_zerop (arg0))
  1238.     return build (NEGATE_EXPR, TREE_TYPE (expr), arg1);
  1239.       if (integer_zerop (arg1))
  1240.     return convert (TREE_TYPE (expr), arg0);
  1241.       /* Fold &x - &x.  This can happen from &x.foo - &x.  */
  1242.       if (operand_equal_p (arg0, arg1))
  1243.     return convert (TREE_TYPE (t), integer_zero_node);
  1244.       goto associate;
  1245.  
  1246.     case MULT_EXPR:
  1247.       if (!loses && integer_zerop (arg0))
  1248.     return convert (TREE_TYPE (expr), arg0);
  1249.       if (!loses && integer_zerop (arg1))
  1250.     return convert (TREE_TYPE (expr), arg1);
  1251.       if (integer_onep (arg0))
  1252.     return convert (TREE_TYPE (expr), arg1);
  1253.       if (integer_onep (arg1))
  1254.     return convert (TREE_TYPE (expr), arg0);
  1255.       goto associate;
  1256.  
  1257.     case BIT_IOR_EXPR:
  1258.       if (!loses && integer_all_onesp (arg0))
  1259.     return convert (TREE_TYPE (expr), arg0);
  1260.       if (!loses && integer_all_onesp (arg1))
  1261.     return convert (TREE_TYPE (expr), arg1);
  1262.     case BIT_XOR_EXPR:
  1263.       if (integer_zerop (arg0))
  1264.     return convert (TREE_TYPE (expr), arg1);
  1265.       if (integer_zerop (arg1))
  1266.     return convert (TREE_TYPE (expr), arg0);
  1267.       goto associate;
  1268.  
  1269.     case BIT_AND_EXPR:
  1270.       if (integer_all_onesp (arg0))
  1271.     return convert (TREE_TYPE (expr), arg1);
  1272.       if (integer_all_onesp (arg1))
  1273.     return convert (TREE_TYPE (expr), arg0);
  1274.       if (!loses && integer_zerop (arg0))
  1275.     return convert (TREE_TYPE (expr), arg0);
  1276.       if (!loses && integer_zerop (arg1))
  1277.     return convert (TREE_TYPE (expr), arg1);
  1278.       /* Simplify (int)((unsigned char)x & 0x377) into (int)(unsigned char)x.  */
  1279.       if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
  1280.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
  1281.     {
  1282.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
  1283.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  1284.           && (~TREE_INT_CST_LOW (arg0) & ((1 << prec) - 1)) == 0)
  1285.         return convert (TREE_TYPE (expr), arg1);
  1286.     }
  1287.       if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
  1288.       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
  1289.     {
  1290.       int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
  1291.       if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_INT
  1292.           && (~TREE_INT_CST_LOW (arg1) & ((1 << prec) - 1)) == 0)
  1293.         return convert (TREE_TYPE (expr), arg0);
  1294.     }
  1295.       goto associate;
  1296.  
  1297.     case BIT_ANDTC_EXPR:
  1298.       if (integer_all_onesp (arg0))
  1299.     return convert (TREE_TYPE (expr), arg1);
  1300.       if (integer_zerop (arg1))
  1301.     return convert (TREE_TYPE (expr), arg0);
  1302.       if (!loses && integer_zerop (arg0))
  1303.     return convert (TREE_TYPE (expr), arg0);
  1304.       if (!loses && integer_all_onesp (arg1))
  1305.     return combine (code, arg1, arg1);
  1306.       goto binary;
  1307.  
  1308.     case TRUNC_DIV_EXPR:
  1309.     case ROUND_DIV_EXPR:
  1310.     case FLOOR_DIV_EXPR:
  1311.     case CEIL_DIV_EXPR:
  1312.     case RDIV_EXPR:
  1313.       if (integer_onep (arg1))
  1314.     return convert (TREE_TYPE (expr), arg0);
  1315.       goto binary;
  1316.  
  1317.     case CEIL_MOD_EXPR:
  1318.     case FLOOR_MOD_EXPR:
  1319.     case ROUND_MOD_EXPR:
  1320.     case TRUNC_MOD_EXPR:
  1321.       if (!loses && integer_onep (arg1))
  1322.     return combine (code, arg1, arg1);
  1323.       goto binary;
  1324.  
  1325.     case LSHIFT_EXPR:
  1326.     case RSHIFT_EXPR:
  1327.     case LROTATE_EXPR:
  1328.     case RROTATE_EXPR:
  1329.       if (integer_zerop (arg1))
  1330.     return convert (TREE_TYPE (expr), arg0);
  1331.       goto binary;
  1332.  
  1333.     case MIN_EXPR: case MAX_EXPR:
  1334.       goto associate;
  1335.  
  1336.     case TRUTH_NOT_EXPR:
  1337.       if (wins)
  1338.     {
  1339.       if (TREE_CODE (arg0) == INTEGER_CST)
  1340.         {
  1341.           t = build_int_2 ((TREE_INT_CST_LOW (arg0) == 0
  1342.                 && TREE_INT_CST_HIGH (arg0) == 0),
  1343.                    0);
  1344.           TREE_TYPE (t) = integer_type_node;
  1345.         }
  1346.       if (TREE_CODE (arg0) == REAL_CST)
  1347.         {
  1348.           t = build_int_2 (TREE_REAL_CST (arg0) == 0, 0);
  1349.           TREE_TYPE (t) = integer_type_node;
  1350.         }
  1351.     }
  1352.       return t;
  1353.  
  1354.     case TRUTH_AND_EXPR:
  1355.     case TRUTH_ANDIF_EXPR:
  1356.       if (wins)
  1357.     {
  1358.       if (TREE_CODE (arg0) == INTEGER_CST
  1359.           && TREE_CODE (arg1) == INTEGER_CST)
  1360.         t = build_int_2 (((TREE_INT_CST_LOW (arg0) || TREE_INT_CST_HIGH (arg0))
  1361.                   && (TREE_INT_CST_LOW (arg1) || TREE_INT_CST_HIGH (arg1))),
  1362.                  0);
  1363.       if (TREE_CODE (arg0) == REAL_CST
  1364.           && TREE_CODE (arg1) == REAL_CST)
  1365.         t = build_int_2 ((TREE_REAL_CST (arg0) && TREE_REAL_CST (arg1)),
  1366.                  0);
  1367.       TREE_TYPE (t) = TREE_TYPE (expr);
  1368.     }
  1369.       return t;
  1370.  
  1371.     case TRUTH_OR_EXPR:
  1372.     case TRUTH_ORIF_EXPR:
  1373.       if (wins)
  1374.     {
  1375.       if (TREE_CODE (arg0) == INTEGER_CST
  1376.           && TREE_CODE (arg1) == INTEGER_CST)
  1377.         t = build_int_2 (((TREE_INT_CST_LOW (arg0) || TREE_INT_CST_HIGH (arg0))
  1378.                   || (TREE_INT_CST_LOW (arg1) || TREE_INT_CST_HIGH (arg1))),
  1379.                  0);
  1380.       if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
  1381.         t = build_int_2 ((TREE_REAL_CST (arg0) || TREE_REAL_CST (arg1)),
  1382.                  0);
  1383.       TREE_TYPE (t) = TREE_TYPE (expr);
  1384.     }
  1385.       return t;
  1386.  
  1387.     case EQ_EXPR:
  1388.     case NE_EXPR:
  1389.     case LT_EXPR:
  1390.     case GT_EXPR:
  1391.     case LE_EXPR:
  1392.     case GE_EXPR:
  1393.       /* If one arg is a constant integer, put it last.  */
  1394.       if (TREE_CODE (arg0) == INTEGER_CST
  1395.       && TREE_CODE (arg1) != INTEGER_CST)
  1396.     {
  1397.       TREE_OPERAND (t, 0) = arg1;
  1398.       TREE_OPERAND (t, 1) = arg0;
  1399.       arg0 = TREE_OPERAND (t, 0);
  1400.       arg1 = TREE_OPERAND (t, 1);
  1401.       switch (code)
  1402.         {
  1403.         case GT_EXPR:
  1404.           code = LT_EXPR;
  1405.           break;
  1406.         case GE_EXPR:
  1407.           code = LE_EXPR;
  1408.           break;
  1409.         case LT_EXPR:
  1410.           code = GT_EXPR;
  1411.           break;
  1412.         case LE_EXPR:
  1413.           code = GE_EXPR;
  1414.           break;
  1415.         }
  1416.       TREE_CODE (t) = code;
  1417.     }
  1418.  
  1419.       /* Convert foo++ == CONST into ++foo == CONST + INCR.
  1420.      First, see if one arg is constant; find the constant arg
  1421.      and the other one.  */
  1422.       {
  1423.     tree constop = 0, varop;
  1424.     tree *constoploc;
  1425.  
  1426.     if (TREE_LITERAL (arg1))
  1427.       constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0;
  1428.     if (TREE_LITERAL (arg0))
  1429.       constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1;
  1430.  
  1431.     if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
  1432.       {
  1433.         tree newconst
  1434.           = fold (build (PLUS_EXPR, TREE_TYPE (t),
  1435.                  constop, TREE_OPERAND (varop, 1)));
  1436.         /* This optimization is invalid for ordered comparisons
  1437.            if CONST+INCR overflows!
  1438.            We can assume that address + integer will not overflow.  */
  1439.         if (TREE_CODE (newconst) != INTEGER_CST
  1440.         || ! tree_int_cst_lt (newconst, constop)
  1441.         || code == EQ_EXPR || code == NE_EXPR)
  1442.           {
  1443.         TREE_CODE (varop) = PREINCREMENT_EXPR;
  1444.         *constoploc = newconst;
  1445.         return t;
  1446.           }
  1447.       }
  1448.     else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
  1449.       {
  1450.         tree newconst
  1451.           = fold (build (MINUS_EXPR, TREE_TYPE (t),
  1452.                  constop, TREE_OPERAND (varop, 1)));
  1453.         if (TREE_CODE (newconst) != INTEGER_CST
  1454.         || tree_int_cst_lt (newconst, constop)
  1455.         || code == EQ_EXPR || code == NE_EXPR)
  1456.           {
  1457.         TREE_CODE (varop) = PREDECREMENT_EXPR;
  1458.         *constoploc = newconst;
  1459.         return t;
  1460.           }
  1461.       }
  1462.       }
  1463.  
  1464.       /* Change X >= CST to X > (CST - 1) if CST is positive.  */
  1465.       if (TREE_CODE (arg1) == INTEGER_CST
  1466.       && ! tree_int_cst_lt (arg1, integer_one_node))
  1467.     {
  1468.       switch (TREE_CODE (t))
  1469.         {
  1470.         case GE_EXPR:
  1471.           TREE_CODE (t) = GT_EXPR;
  1472.           arg1 = combine (MINUS_EXPR, arg1, integer_one_node);
  1473.           TREE_OPERAND (t, 1) = arg1;
  1474.           break;
  1475.  
  1476.         case LT_EXPR:
  1477.           TREE_CODE (t) = LE_EXPR;
  1478.           arg1 = combine (MINUS_EXPR, arg1, integer_one_node);
  1479.           TREE_OPERAND (t, 1) = arg1;
  1480.         }
  1481.     }
  1482.  
  1483.       /* An unsigned comparison against 0 can be simplified.  */
  1484.       if (integer_zerop (arg1)
  1485.       && (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
  1486.           || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE)
  1487.       && TREE_UNSIGNED (TREE_TYPE (arg1)))
  1488.     {
  1489.       switch (TREE_CODE (t))
  1490.         {
  1491.         case GT_EXPR:
  1492.           TREE_CODE (t) = NE_EXPR;
  1493.           break;
  1494.         case LE_EXPR:
  1495.           TREE_CODE (t) = EQ_EXPR;
  1496.           break;
  1497.         case GE_EXPR:
  1498.           return build (COMPOUND_EXPR, integer_type_node,
  1499.                 arg0, integer_one_node);
  1500.         case LT_EXPR:
  1501.           return build (COMPOUND_EXPR, integer_type_node,
  1502.                 arg0, integer_zero_node);
  1503.         }
  1504.     }
  1505.  
  1506.       /* To compute GT, swap the arguments and do LT.
  1507.      To compute GE, do LT and invert the result.
  1508.      To compute LE, swap the arguments, do LT and invert the result.
  1509.      To compute NE, do EQ and invert the result.  */
  1510.       if (code == LE_EXPR || code == GT_EXPR)
  1511.     {
  1512.       register tree temp = arg0;
  1513.       arg0 = arg1;
  1514.       arg1 = temp;
  1515.     }
  1516.  
  1517.       /* Compute a result for LT or EQ if args permit.  */
  1518.       if (TREE_CODE (arg0) == INTEGER_CST
  1519.       && TREE_CODE (arg1) == INTEGER_CST)
  1520.     {
  1521.       if (code == EQ_EXPR || code == NE_EXPR)
  1522.         t = build_int_2
  1523.           (TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1)
  1524.            && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1),
  1525.            0);
  1526.       else
  1527.         t = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
  1528.                   ? INT_CST_LT_UNSIGNED (arg0, arg1)
  1529.                   : INT_CST_LT (arg0, arg1)),
  1530.                  0);
  1531.     }
  1532.       else if (TREE_CODE (arg0) == REAL_CST
  1533.            && TREE_CODE (arg1) == REAL_CST)
  1534.     {
  1535.       if (code == EQ_EXPR || code == NE_EXPR)
  1536.         t = build_int_2 (TREE_REAL_CST (arg0) == TREE_REAL_CST (arg1), 0);
  1537.       else
  1538.         t = build_int_2 (TREE_REAL_CST (arg0) < TREE_REAL_CST (arg1), 0);
  1539.     }
  1540.       else
  1541.     return t;
  1542.  
  1543.       /* If we wanted ...-or-equal, invert the result.  */
  1544.       if (code == GE_EXPR || code == LE_EXPR || code == NE_EXPR)
  1545.     TREE_INT_CST_LOW (t) ^= 1;
  1546.       TREE_TYPE (t) = TREE_TYPE (expr);
  1547.       return t;
  1548.  
  1549.     COND_EXPR:
  1550.       if (TREE_LITERAL (arg0))
  1551.     return TREE_OPERAND (expr, (integer_zerop (arg0) ? 2 : 1));
  1552.       return t;
  1553.  
  1554.     default:
  1555.       return t;
  1556.     } /* switch (code) */
  1557. }
  1558.